package com.dm.library.http.base;

import android.content.Context;

import com.dm.library.http.HttpParams;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.NetworkUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: sushuo
 * 修改时间: 2016-12-20
 */
public abstract class HttpBase {

    protected static final String TAG = "BaseHttp";

    public <T> void post(Context context, String url, final RequestCallBack<T> callback) {
        post(context, url, null, callback);
    }

    public <T> void post(Context context, String url, HttpParams params, final RequestCallBack<T> callback) {
        if (url == null) throw new IllegalArgumentException("url may not be null");
        if (params == null) {
            params = new HttpParams();
        }
        if (!validateNetwork(context)) {
            doNoNetworkAction(context, callback);
//            return;
        }
        if (validateLogin(context, url)) {
            postData(context, url, params, callback);
        }
    }

    public <T> void postString(Context context, String url, String body, final RequestCallBack<T> callback) {
        if (url == null) throw new IllegalArgumentException("url may not be null");
        if (!validateNetwork(context)) {
            doNoNetworkAction(context, callback);
//            return;
        }
        if (validateLogin(context, url)) {
            postData(context, url, body, callback);
        }
    }


    public <T> void get(Context context, String url, final RequestCallBack<T> callback) {
        get(context, url, null, callback);
    }

    public <T> void get(Context context, String url, HttpParams params, final RequestCallBack<T> callback) {
        if (url == null) throw new IllegalArgumentException("url may not be null");
        if (params == null) {
            params = new HttpParams();
        }
        if (!validateNetwork(context)) {
            doNoNetworkAction(context, callback);
//            return;
        }
        if (validateLogin(context, url)) {
            getData(context, url, params, callback);
        }
    }

    public <T> void put(Context context, String url, final RequestCallBack<T> callback) {
        put(context, url, null, callback);
    }

    public <T> void put(Context context, String url, HttpParams params, final RequestCallBack<T> callback) {
        if (url == null) throw new IllegalArgumentException("url may not be null");
        if (params == null) {
            params = new HttpParams();
        }
        if (!validateNetwork(context)) {
            doNoNetworkAction(context, callback);
//            return;
        }
        if (validateLogin(context, url)) {
            putData(context, url, params, callback);
        }
    }

    public <T> void uploadFile(Context context, String url, String name, Map<String, File> files, final RequestCallBack<T> callback) {
        uploadFile(context, url, null, name, files, callback);
    }

    public <T> void uploadFile(Context context, String url, Map<String, String> params, String name, Map<String, File> files, final RequestCallBack<T> callback) {
        if (url == null) throw new IllegalArgumentException("url may not be null");
        if (params == null) {
            params = new HashMap<>();
        }
        if (!validateNetwork(context)) {
            doNoNetworkAction(context, callback);
//            return;
        }
        if (validateLogin(context, url)) {
            uploadFileData(context, url, params, name, files, callback);
        }
    }

    protected abstract <T> void putData(Context context, String url, HttpParams params, final RequestCallBack<T> callback);

    protected abstract <T> void getData(Context context, String url, HttpParams params, final RequestCallBack<T> callback);

    protected abstract <T> void postData(Context context, String url, HttpParams params, final RequestCallBack<T> callback);

    protected abstract <T> void postData(Context context, String url, String body, final RequestCallBack<T> callback);

    protected abstract <T> void uploadFileData(Context context, String url, Map<String, String> params, String name, Map<String, File> files, final RequestCallBack<T> callback);

    private boolean validateNetwork(Context context) {
        return NetworkUtils.isNetworkAvailable(context);
    }

    private void doNoNetworkAction(Context context, RequestCallBack callback) {
//        ToastUtil.getInstant().show(mContext, "网络连接不可用");
        if (callback != null) {
            callback.onConnectFailure("网络连接不可用");
        }
    }

    private boolean validateLogin(Context context, String url) {
        if (context instanceof LoginCheckListener) {
            LoginCheckListener listener = (LoginCheckListener) context;
            boolean isLogin = listener.checkLogin();
            List<String> urls = listener.getWhiteList();
            DMLog.i(TAG, "validateLogin()-->该URL需要登录：" + urls.contains(url) + "，" + url);
            if (!isLogin && !urls.contains(url)) {
                // 如果没有登录,跳转到登录页
                listener.goLogin();
                return false;
            }
        } else {
            DMLog.d(TAG, "validateLogin() -- the application not implements LoginCheckListener ,so won't check login");
        }
        return true;
    }

    public interface LoginCheckListener {
        void goLogin();

        boolean checkLogin();

        List<String> getWhiteList();
    }

    /**
     * 对code统一进行权限校验
     */
    public interface AuthorizationCheckListener {

        List<String> getInvalidCodes();

        void doInvalidCodeAction(String code, String json);

    }

}
